package com.cy.law.vo;

import java.util.HashMap;
import java.util.Map;

public class RequestResult {
    private int code;
    private String message;
    private Object data;

    public RequestResult() {
    }

    public RequestResult(int code) {
        this.code = code;
    }

    public RequestResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public RequestResult(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public RequestResult(int code, Object data) {
        this.code = code;
        this.data = data;
    }

    public RequestResult(CodeMessage cm) {
        this.code = cm.getCode();
        this.message = cm.getMessage();
    }


    public static RequestResult newAjaxResult(boolean succ) {
        if (succ) {
            return new0();
        } else {
            return newUndefined();
        }
    }


    public static RequestResult new0() {
        return new RequestResult(0);
    }

    public static RequestResult new1() {
        return new RequestResult(1);
    }

    public static RequestResult newUndefined() {
        return new RequestResult(CodeMessage.UNDEFINED);
    }

    public static HashMap<String, Object> map() {
        return new HashMap<String, Object>();
    }

    public static HashMap<String, Object> map(String k, Object o) {
        return new HashMap<String, Object>(16) {
            {
                this.put(k, o);
            }
        };
    }

    public int getCode() {
        return code;
    }

    public RequestResult setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public RequestResult setMessage(String message) {
        this.message = message;
        return this;
    }

    public RequestResult setCodeAndMessage(int code) {
        this.message = CodeMessage.msgOf(code);
        this.code = code;
        return this;
    }

    public Object getData() {
        return data;
    }

    public RequestResult setData(Object data) {
        this.data = data;
        return this;
    }

    public RequestResult setCodeMessage(CodeMessage cm) {
        this.code = cm.getCode();
        this.message = cm.getMessage();
        return this;
    }

    public RequestResult setMapData(String key, Object val) {
        this.data = new HashMap<String, Object>(16) {{
            this.put(key, val);
        }};
        return this;
    }

    @SuppressWarnings("unchecked")
    public RequestResult addMapData(String key, Object val) {
        if (this.data instanceof Map) {
            ((Map) this.data).put(key, val);
        }
        return this;
    }
}